//引入考试题目model
const { examTypeModel } = require("../models/examTypeModel");
const {
    knowledgePointsModel
} = require("../models/knowledgePointsModel");
const {
    questionsModel
} = require("../models/questionsModel");

const {
    usersModel
} = require("../models/usersModel");
const {
    testsModel
} = require("../models/testsModel");

//获取考生和试卷信息以试题
async function getExamUser(req, res, next) {
    //根据用户id
    const user_id = req.body.user_id;
    const user = await usersModel.find({
        _id: user_id
    });
    //根据tests_id找到对应试卷信息
    const { tests_id } = req.body;
    const test = await testsModel.find({
        _id: tests_id
    }).populate('KnowledgePointId').populate('TypeId');

    const know = test[0].KnowledgePointId;

    const questions = await questionsModel.find({ KnowledgePointId: know._id }).populate('KnowledgePointId');

    //发送该用户信息和试卷以及试题
    res.send({
        user: user,
        test: test,
        questions: questions,
        code: 1
    });
}

//计算总分数
async function getTotleScore(req, res, next) {
    let { quesArr } = req.body;

    quesArr = JSON.parse(quesArr);
    let totleScore = 0;
    let allScore=0;
    let key;
    for (let i = 0; i < quesArr.length; i++) {
        let _id = quesArr[i]._id;
        let question = await questionsModel.find({
            _id
        });
        allScore+=question[0].score;
        if (quesArr[i].selectVal == "A") {
            key = 0
        } else if (quesArr[i].selectVal == "B") {
            key = 1;
        } else if (quesArr[i].selectVal == "C") {
            key = 2;
        } else {
            key = 3;
        }
        if (key == question[0].RightKey) {
            totleScore += question[0].score;
        }
    }

    res.send({
        allScore,
        totleScore,
        code: 1
    });
}
module.exports = {
    getExamUser,
    getTotleScore
}